<?php
/**
 * Description of ItemEditor
 * Klasa wykorzystana w kodzie z zendcastform
 * @author jon
 */
class App_forms_ItemEditor extends Zend_Form
{

	public function __construct(int $itemId , Zend_Session_Namespace $session, $mode = 'create')
	{


		$this->name = new Zend_Form_Element_Text('name');
		$this->name->setLabel("item name:")->setRequired(true);

		$this->description = new Zend_Form_Element_Textarea('description');
		$this->description->setLabel('description');
		$this->id = new Zend_Form_Element_Hidden('id');

		$this->id->setValue($itemId);

		if (array_key_exists($itemId, $session->items))
			$this->mapItemToForm($session->items[$itemId]);


		$this->submit = new Zend_Form_Element_Submit($mode);

		$this->addElements(array(
				$this->name,
				$this->description,
				$this->id
		));


		$this->setDecorators(array(
			
		    'FormElements',
		    array('HtmlTag', array('tag' => 'dl')),
		    'Form'

		));
	}

	/**
	 * Odebranie elementu z sesji i z  niego ustawienie wartosci  
	 * potrzebne przy populate formularza
	 */
	private function mapItemToForm(App_ItemDto $item)
	{
		$this->name->setValue($item->name);
		$this->description->setValue($item->description);
		$this->id->setValue($item->id);
	}
	
	public function SaveItem()
	{
			return $this->mapFormToItemDto();
	}
	
	private function mapFormToItemDto()
	{
		return new App_ItemDto(stripslashes($this->name->getValue()) , stripslashes($this->description->getValue()), stripslashes($this->id->getValue()));
		
	}

}